package MasterManagers.SocketManager;

import MasterManagers.TableManager;

import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class ClientCMD {
    private TableManager tableManager;
    private Socket socket;
    public ClientCMD(TableManager tableManager, Socket socket){
        this.tableManager = tableManager;
        this.socket = socket;
    }

    /**
     * 处理Client发来的消息
     * @param cmd
     * @return String 发送给这个客户端的消息
     */
    public String processClientCommand(String cmd){
//        System.out.println("收到Client发来的信息 " + cmd);
        String result = "";
        String tableName = "";
        String parsed = cmd.substring(cmd.indexOf("]")+1);//去掉开头的前缀
        /**
         *         <C2M>[1]  返回三个
         *         <C2M>[2]  create操作，使用负载均衡选出三个来返回
         *         <C2M>[3]  返回所有表名
         *         <C2M>[7]  master本地显示结果
         *         <C2M>[10] 添加client到本地列表
         */
        //=======================测试使用========================
//        List<String> ips = new ArrayList<>();
//        ips.add("1.1.1.1:1111");
//        ips.add("2.2.2.2:2222");
//        ips.add("3.3.3.3:3333");
        //======================================================
        if (cmd.startsWith("<C2M>[1]")) { // sql操作

            tableName = parsed;
            System.out.println("tableName:" + tableName);
            List<String> ips = tableManager.getInetAddress(tableName); //三个ip
            String toClient = "<M2C>[2]" + tableName;
            if(ips == null){
                toClient += ',';
            }else{
                for(String ip: ips){
                    toClient += ',' + ip;
                }
            }
            result = toClient;
            System.out.println("Send to Client: "+toClient);

        } else if (cmd.startsWith("<C2M>[2]")) { // create，需要向client以及三个region分别发送消息
            tableName = parsed.split(",")[0];
            System.out.println("tableName:" + tableName);
            List<String> ips = tableManager.getIdealServer(); //三个ip
            String toClient = "<M2C>[2]" + tableName;
            for(String ip: ips){
                toClient += ',' + ip;
            }
            result = toClient;
            System.out.println("Send to Client: "+result);
            System.out.println("Send to Region: " + "<M2R>[4]" + parsed);
            for(String ip : ips){
                for(SocketThread s : this.tableManager.getSocketThread(ip)){
                    s.send("<M2R>[4]" + parsed);
                }
            }
        }else if(cmd.startsWith("<C2M>[3]")){ //show tables
            String toClient = "<M2C>[3]" + tableManager.showTables();
            result = toClient;
            System.out.println("Send to Client: " + toClient);
        }else if(cmd.startsWith("<C2M>[7]")){ //前面已经输出过了，不需要再输出了

        }else if(cmd.startsWith("<C2M>[10]")){//添加到client列表
            String ip = socket.getInetAddress().getHostAddress();
            tableManager.addClient(ip);
        }
        return result;
    }

}
